package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 分页条件查询
     * @param deviceDataPageReqDto
     * @return
     */
    @Override
    public PageResponse<DeviceDataVo> selectByPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(),deviceDataPageReqDto.getPageSize());

        //转换时间的类型
        Long startTime = deviceDataPageReqDto.getStartTime();
        Long endTime = deviceDataPageReqDto.getEndTime();

        deviceDataPageReqDto.setStartDate(ObjectUtil.isEmpty(startTime) ? null: LocalDateTimeUtil.of(startTime));
        deviceDataPageReqDto.setEndDate(ObjectUtil.isEmpty(endTime) ? null: LocalDateTimeUtil.of(endTime));

        Page<DeviceDataVo> page = deviceDataMapper.selectByPage(deviceDataPageReqDto);
        return PageResponse.of(page,DeviceDataVo.class);
    }

    /**
     * 按日查询设备数据
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime startTime1 =   LocalDateTimeUtil.of(startTime);
        LocalDateTime endTime1 =   LocalDateTimeUtil.of(endTime);
        List<DeviceDataGraphVo> deviceDataGraphVos =deviceDataMapper.queryDeviceDataListByDay(iotId,functionId,startTime1,endTime1);
        //创建一个24小时的list从开始开始计算
        List<DeviceDataGraphVo> list = DeviceDataGraphVo.dayInstance(startTime1);
        //创建一个Map用来给时段为0的数据赋值
        Map<String, Double> map = deviceDataGraphVos.stream()
                .collect(Collectors
                        .toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        list.forEach(d -> {
            //从map获取值，如果为空，则给默认为0
            Double val = map.get(d.getDateTime()) == null ? 0.0 : map.get(d.getDateTime());
            d.setDataValue(val);
        });

        return list;
    }

    /**
     * 按周查询设备数据
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime startTime1 =   LocalDateTimeUtil.of(startTime);
        LocalDateTime endTime1 =   LocalDateTimeUtil.of(endTime);
        List<DeviceDataGraphVo> deviceDataGraphVos =deviceDataMapper.queryDeviceDataListByWeek(iotId,functionId,startTime1,endTime1);
        //创建一个7天的list从开始开始计算
        List<DeviceDataGraphVo> list = DeviceDataGraphVo.weekInstance(startTime1);
        Map<String, Double> map = deviceDataGraphVos.stream()
                .collect(Collectors
                        .toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));
        list.forEach(d -> {
            if(map.get(d.getDateTime())!=null){
                Double dataValue = map.get(d.getDateTime());
                d.setDataValue(dataValue);
            }

        });
        return list;

    }
}
